package org.example.webserver.service.serviceImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.webserver.dao.CommodityDao;
import org.example.webserver.dao.OrderDao;
import org.example.webserver.dao.OrderStateDao;
import org.example.webserver.dao.UserDao;
import org.example.webserver.pojo.Commodity;
import org.example.webserver.pojo.Order;
import org.example.webserver.pojo.OrderState;
import org.example.webserver.pojo.User;
import org.example.webserver.service.OrderService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;
    @Resource
    OrderStateDao orderStateDao;
    @Resource
    CommodityDao commodityDao;
    @Resource
    UserDao userDao;

    @Override
    public Order buy(Order order) {
        Commodity commodity=commodityDao.findCommodityById(order.getCommodityid());
        User user=userDao.findByUser(commodity.getMerchant());
        order.setMerchantid(user.getId());
        orderDao.save(order);
        System.out.println(order.getId());
        OrderState orderState=new OrderState("placed_an_order",order.getId());
        orderStateDao.save(orderState);
        return order;
    }

    @Override
    public JSONArray findOrderByUser(Long id) {
        List<Order> orderList=orderDao.findByBuyerid(id);
        JSONArray jsonArray=new JSONArray();
        for(Order o:orderList){
            User user=userDao.findById(o.getBuyerid());
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",o.getId());
            jsonObject.put("buyerid",o.getBuyerid());
            jsonObject.put("buyer",user.getUser());
            jsonObject.put("consignee",o.getConsignee());
            jsonObject.put("phone_num",o.getPhone_num());
            jsonObject.put("address",o.getAddress());
            jsonObject.put("quantity",o.getQuantity());
            jsonObject.put("commodity",o.getCommodity());
            jsonObject.put("commodityid",o.getCommodityid());
            jsonObject.put("amount",o.getAmount());
            OrderState orderState=orderStateDao.findBy_id(o.getId());
            jsonObject.put("state",orderState.getState());
            jsonObject.put("couriernum",orderState.getCouriernum());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public JSONArray findOrderByMerchant(String user) {
        User user0= userDao.findByUser(user);
        JSONArray jsonArray=new JSONArray();
        List<Order> orderList=orderDao.findByMerchantid(user0.getId());
        for(Order o:orderList){
            User user1=userDao.findById(o.getBuyerid());
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",o.getId());
            jsonObject.put("buyerid",o.getBuyerid());
            jsonObject.put("buyer",user1.getUser());
            jsonObject.put("consignee",o.getConsignee());
            jsonObject.put("phone_num",o.getPhone_num());
            jsonObject.put("address",o.getAddress());
            jsonObject.put("quantity",o.getQuantity());
            jsonObject.put("commodity",o.getCommodity());
            jsonObject.put("commodityid",o.getCommodityid());
            jsonObject.put("amount",o.getAmount());
            OrderState orderState=orderStateDao.findBy_id(o.getId());
            jsonObject.put("state",orderState.getState());
            jsonObject.put("couriernum",orderState.getCouriernum());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public OrderState send(OrderState orderState) {
        OrderState orderState1=orderStateDao.findBy_id(orderState.get_id());
        orderState1.setState("shipped");
        orderState1.setCouriernum(orderState.getCouriernum());
        orderStateDao.save(orderState1);
        return orderState1;
    }

    @Override
    public OrderState cancel(int id) {
        OrderState orderState=orderStateDao.findBy_id((long) id);
        if(orderState.getState().equals("placed_an_order")){
            orderState.setState("cancelled");
            orderState.setCouriernum(null);
            orderStateDao.save(orderState);
            return orderState;
        }else {
            return null;
        }
    }
}
